import time
import os
import shutil

def traversal_dir(root_path):
    files_path = []
    files_list = os.listdir(root_path)
    for file in files_list:
        path_string = root_path + '\\' + str(file)
        if os.path.isfile(path_string) :#如果是文件
            files_path.append(path_string)
        elif os.path.isdir(path_string) :#如果是文件夹
            files_path.extend(traversal_dir(path_string))
    return files_path

def replace(file_path, old_str, new_str):
    try:
        f = open(file_path,'r+')
        all_lines = f.readlines()
        f.seek(0)
        f.truncate()
        for line in all_lines:
            line = line.replace(old_str, new_str)
        f.write(line)
        f.close()
    except Exception as e:
        print(e)

if __name__== '__main__':
    project_path = "G:\\AuxHotWater_GPRS"
    files_path = traversal_dir(project_path)
    i = 0
    for files in files_path:
        if files.endswith(".c") or files.endswith(".h") :
            replace(files, "哈哈", "hh")
            print(str(files))